package models

import (
	"fmt"
	"github.com/jinzhu/gorm"
)

type Tag struct {
	Model

	Name       string `json:"name"`
	CreatedBy  string `json:"created_by"`
	ModifiedBy string `json:"modified_by"`
	State      int    `json:"state"`
}

//func (tag *Tag) BeforeCreate(scope *gorm.Scope) error{
//	scope.SetColumn("CreateOn", time.Now().Unix())
//	return nil
//}
//
//func (tag *Tag) BeforeUpdate(scope *gorm.Scope) error{
//	scope.SetColumn("ModifiedOn", time.Now().Unix())
//	return nil
//}

func GetTags(pageNum int, pageSize int, maps interface{}) ([]Tag, error) {
	var (
		tags []Tag
		err error
	)

	if pageSize >0  && pageNum >0 {
		err = db.Where(maps).Offset(pageNum).Limit(pageSize).Find(&tags).Error
	}else{
		err = db.Where(maps).Find(&tags).Error
	}

	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	return tags,nil
}

func GetTagTotal(maps interface{}) (int, error) {
	var count int
	if err := db.Model(&Tag{}).Where(maps).Count(&count).Error; err != nil {
		return 0, err
	}
	return count, nil
}

/*
 是否存在重复的Tag
 */
func ExistTagByName(name string) (bool, error) {
	var tag Tag
	err := db.Select("id").Where("name=? AND deleted_on=?", name, 0).First(&tag).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return false, err
	}
	if tag.ID > 0 {
		return true, nil
	}

	return false, nil
}

//是否存在重复ID
func ExistTagByID(id int) (bool, error) {
	fmt.Println("id : ",id)
	var tag Tag
	err := db.Select("id").Where("id=? AND deleted_on = ?", id, 0).First(&tag).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return false, err
	}
	if tag.ID > 0 {
		return true, nil
	}

	return false, nil
}

//新增
func AddTag(name string, state int, createBy string) error {
	tag := &Tag{
		Name:      name,
		State:     state,
		CreatedBy: createBy,
	}
	if err := db.Create(&tag).Error; err != nil {
		return err
	}
	return nil
}

//编辑
func EditTag(id int, data interface{}) error {
	if err := db.Model(&Tag{}).Where("id=? AND deleted_on = ?", id, 0).Updates(data).Error; err != nil {
		return err
	}
	return nil
}

//删除
func DeleteTag(id int) error {
	if err := db.Where("id=?", id).Delete(&Tag{}).Error; err != nil {
		return err
	}
	return nil
}

//清除所有
func CleanAllTag() (bool, error) {
	if err := db.Unscoped().Where("deleted_on != ?", 0).Delete(&Tag{}).Error; err != nil {
		return false, err
	}
	return true, nil
}
